export default {
  data() {
    // 库存预警值自定义验证
    const validateStockWarn = (rule, value, callback) => {
      if (value <= 0 || value > 100) {
        callback(new Error('请输入1-100之间的整数'))
      } else {
        callback()
      }
    }
    // 虚拟销量自定义验证
    const validVirtualSales = (rule, value, callback) => {
      if (value <= 0 || value > 1000) {
        callback(new Error('请输入1-1000之间的整数'))
      } else {
        callback()
      }
    }

    // 商品编号
    const validGoodsNumCode = (rule, value, callback) => {
      if (!/^[A-Za-z0-9]{3,18}$/.test(value)) {
        callback(new Error('3-18字符以内，只能输入数字+英文'))
      } else {
        callback()
      }
    }

    // 分类
    const validateCategoryId = (rule, value, callback) => {
      if (value.length < 2) {
        callback(new Error('请选择二级分类'))
      } else if (value.length < 3) {
        callback(new Error('请选择三级分类'))
      } else {
        callback()
      }
    }

    // 限购数量
    const validateByLimit = (rule, value, callback) => {
      let regx = /^([0-9]|[1-9]\d|1\d{2}|200)$/
      if (!regx.test(value)) {
        callback(new Error('请输入0-200的整数'))
      } else {
        callback()
      }
    }

    // 商品编码
    const validCode = (rule, value, callback) => {
      if (!/^[A-Za-z0-9\-]{1,15}$/.test(value) && value !== '') {
        callback(new Error('15位以内，只能输入数字，英文加-'))
      } else {
        callback()
      }
    }

    return {
      rules: {
        name: [
          {
            required: true,
            message: '请输入商品名称',
            trigger: 'blur'
          },
          {
            max: 100,
            message: '商品名称在100字以内',
            trigger: 'blur'
          }
        ],
        // 商品分类
        categoryId: [
          {
            type: 'array',
            required: true,
            message: '请选择商品分类',
            trigger: 'change'
          },
          { validator: validateCategoryId, trigger: 'change' }
        ],
        images: [
          {
            type: 'array',
            required: true,
            message: '请输上传商品图片',
            trigger: 'change'
          }
        ],
        // 供应商id
        supplierId: [
          {
            required: true,
            message: '请选择供应商',
            trigger: 'change'
          }
        ],
        // 品牌id
        brandId: [
          {
            required: true,
            message: '请选择商品品牌',
            trigger: 'blur'
          }
        ],
        // 上架平台
        platform: [
          {
            required: true,
            message: '请选择上架平台',
            trigger: 'blur'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入商品排序',
            trigger: 'blur'
          },
          {
            pattern: /^[1-9]\d{0,3}$/,
            message: '请输入1-9999'
          }
        ],
        // supid
        supplySpuId: [
          {
            required: true,
            message: '请输入SPUID',
            trigger: 'blur'
          },
          {
            pattern: /^\d{1,12}$/,
            message: '12字符以内，只能输入纯数字',
            trigger: 'blur'
          }
        ],
        supplyId: [
          {
            required: true,
            message: '请选择供应链',
            trigger: 'change'
          }
        ],
        // 库存预警值
        stockWarningQuantity: [
          {
            required: true,
            message: '请输入库存预警值',
            trigger: 'blur'
          },
          { type: 'number', message: '请输入数字', trigger: ['blur', 'change'] },

          { validator: validateStockWarn, trigger: ['blur', 'change'] }
        ],
        // 虚拟销量
        virtualSalesQuantity: [
          {
            required: true,
            message: '请输入虚拟销量',
            trigger: 'blur'
          },
          { type: 'number', message: '请输入数字', trigger: ['blur', 'change'] },
          { validator: validVirtualSales, trigger: ['blur', 'change'] }
        ],
        // SPUID
        spuid: [
          {
            required: true,
            message: '请输入SPUID',
            trigger: 'blur'
          },
          {
            pattern: /^\d{1,}$/,
            message: '只能输入纯数字',
            trigger: 'blur'
          },
          {
            max: 12,
            message: '限制输入12字符以内',
            trigger: 'blur'
          }
        ],
        goodsNumCode: [
          {
            required: true,
            message: '请输入商品编号',
            trigger: 'blur'
          },
          { validator: validGoodsNumCode, trigger: 'blur' }
        ],
        expressTemplateId: [
          {
            required: true,
            message: '请选择运费模版',
            trigger: 'change'
          }
        ],
        buyLimit: [
          {
            required: true,
            message: '请输入限制购买数量',
            trigger: 'blur'
          },
          {
            validator: validateByLimit,
            trigger: 'blur'
          }
        ],
        code: [{ validator: validCode, trigger: 'blur' }],
        remake: [
          {
            min: 2,
            max: 15,
            message: '限制输入2到15字符以内',
            trigger: 'blur'
          }
        ]
      }
    }
  }
}
